// SPDX-License-Identifier: GPL-2.0
/*
 * ONDEMAND: MLOCK
 *
 * (C) 2024.05.01 BuddyZhang1 <buddy.zhang@aliyun.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>

#define MAP_VADDR	0x600000000
#define MAP_SIZE	0x1000

int main()
{
	void *mem;

	/* ALLOC MEMORY */
	mem = mmap((void *)MAP_VADDR, MAP_SIZE,
		   PROT_READ | PROT_WRITE,
		   MAP_ANONYMOUS | MAP_PRIVATE,
		   -1,
		   0);
	if (mem == MAP_FAILED) {
		printf("ERROR: ALLOC MEMORY FAILED.\n");
		return -1;
	}

	/* ONDEMAND: MLOCK */
	mlock(mem, MAP_SIZE);

	/* ACCESS: DONT TRIGGER #PF */
	*(char *)mem = 'B';
	printf("MEMORY: %#lx %c\n", (unsigned long)mem, *(char *)mem);

	/* RECLAIM */
	munmap(mem, MAP_SIZE);

	return 0;
}
